package com.base.myproject.server;

import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;

import com.base.myproject.server.database.MakeCode;
import com.base.myproject.server.database.PoolMan;

public class Task {
	static Task task = null;
	java.util.Properties p = new java.util.Properties();
	final static String ROW_UPDATE ="1";
	final static String ROW_ADD ="2";
	final static String ROW_DELETE ="4";
	final static String ROW_NULLSTATUS ="0";
	String rowstatus="rowstatus";
	boolean ischange = false;
	int keycode=0;
	private Task()
	{
		try {
			getResultSet();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public static Task getinstance()
	{
		if(task==null)
		{
			task = new Task();
		}
		return task;
	}
	private java.util.Properties getResultSet() throws Exception
	{
		keycode = MakeCode.makeCode("task", "icode");
		ResultSet rs = null;
		PoolMan pm = new PoolMan();
		String sql = "select * from task";
		rs = pm.executeQuery(sql);
		 p.clear();
		
		int columncount = rs.getMetaData().getColumnCount();
    	
    	String[] column_names= new String[columncount];
    	for(int i=0;i<columncount;i++)
    	{
    		column_names[i]=rs.getMetaData().getColumnName(i+1);
    	}
    	int i=0;
		while(rs.next())
		{
			
			java.util.Hashtable<String, String> ht = new java.util.Hashtable<String, String>();
			for(int j=0;j<columncount;j++)
			{
				String v = rs.getString(column_names[j]);
				ht.put(column_names[j], v==null?"":v);
				
			}
			ht.put(rowstatus, ROW_NULLSTATUS);
			System.out.println(ht.get(rowstatus)+"aa");
	
			p.put(Integer.parseInt(rs.getString("icode")),ht);
		}
		
		return p;
		
	}
	
	public String P2XMLSTR()
	{
	
		StringBuffer sbf = new StringBuffer();
		sbf.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
		
		Enumeration e = p.keys();
		sbf.append("<records>");
		while(e.hasMoreElements())
		{
			
			int key = Integer.parseInt(e.nextElement().toString());
			java.util.Hashtable<String, String> ht  = (java.util.Hashtable<String, String>)p.get(key);
			Enumeration hte = ht.keys();
			sbf.append("<record>");
			while(hte.hasMoreElements())
			{
				
				String htkey = hte.nextElement().toString();
				String htvalue = ht.get(htkey);
				if(!htkey.equals(rowstatus))
				{
				sbf.append("<"+htkey+">");
				sbf.append(htvalue==null?"":htvalue);
				sbf.append("</"+htkey+">");
				}
			}
			sbf.append("</record>");
			
		}
		sbf.append("</records>");
	System.out.println(sbf);
		return new String(sbf);
	}
	
	@SuppressWarnings("unchecked")
	public void save()
	{
		
			
		Enumeration pe  = p.keys();
		while(pe.hasMoreElements())
		{
		
			
			int key = Integer.parseInt(pe.nextElement().toString());
			
			java.util.Hashtable<String, String> ht  = (java.util.Hashtable<String, String>)(p.get(key));

			if(ht.get(rowstatus).equals(ROW_ADD))
			{
				String sql = "";
				Enumeration hte = ht.keys();
				String keys= "";
				String values="";
				while(hte.hasMoreElements())
				{
					String htkey = hte.nextElement().toString();;
					String htvalue = ht.get(htkey);
					if(!htkey.equals(rowstatus))
					{
						keys +=""+htkey+",";
						values +="'"+htvalue+"',";
					}
				}
				keys = keys.substring(0,keys.length()-1);
				values = values.substring(0,values.length()-1);
				sql = " insert into task ("+keys+") values ("+values+")";
				System.out.println(sql);
			}
			ht.put(rowstatus, ROW_NULLSTATUS);
			
		}
		ischange = false;
	}
	public void delete(String index)
	{
		((java.util.Hashtable<String, String>)p.get(index)).put(rowstatus, ROW_DELETE);
		ischange = true;
	}
	public void add(java.util.Hashtable<String, String> ht )
	{
		keycode++;
		ht.put(rowstatus, ROW_ADD);
		Date d = new Date();
		SimpleDateFormat dateformat= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		SimpleDateFormat dateyyyymmdd= new SimpleDateFormat("yyyy-MM-dd");
		ht.put("predate", dateformat.format(d));
		ht.put("modifydate", dateformat.format(d));
		ht.put("modifydatebak", dateyyyymmdd.format(d));
		p.put(keycode, ht);
		
		ischange = true;
	}
	public void update(int index,java.util.Hashtable<String, String> ht )
	{
		ht.put(rowstatus, ROW_UPDATE);
		Date d = new Date();
		SimpleDateFormat dateformat= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		SimpleDateFormat dateyyyymmdd= new SimpleDateFormat("yyyy-MM-dd");
		
		ht.put("modifydate", dateformat.format(d));
		ht.put("modifydatebak", dateyyyymmdd.format(d));
		p.put(index, ht);
		ischange = true;
	}
	/**
	 * 是否已经改变
	 * @return
	 */
	public boolean isIschange() {
		return ischange;
	}
	public void setIschange(boolean ischange) {
		this.ischange = ischange;
	}
	
	/**
	 * 
	 * @return
	 */
	public java.util.Properties getP() {
		return p;
	}
	public void setP(java.util.Properties p) {
		this.p = p;
	}
	

}
